# -*- coding: utf-8 -*-

from conf.neo4j_cnx import NeoCnxPool

neo_cnx = NeoCnxPool()


def _query_property_by_symptom(tx, symptom_name):
    query = (
        'MATCH (s:Symptom {name: $symptom_name})'
        '-[:HasProperty]'
        '->(p:Property) '
        'RETURN p.name as name, p.uid as uid'
    )
    result = tx.run(query, symptom_name=symptom_name)
    property_list = []
    for record in result:
        property_list.append({'name': record['name'], 'uid': record['uid']})
    return property_list


def _query_option_by_property(tx, property_uid):
    query = (
        'MATCH (p:Property {uid: $property_uid})'
        '-[:HasOption]'
        '->(o:Option) '
        'RETURN o.name as name, o.uid as uid'
    )
    result = tx.run(query, property_uid=property_uid)
    option_list = []
    for record in result:
        option_list.append({'name': record['name'], 'uid': record['uid']})
    return option_list


def _query_option_param(tx, option_uid):
    query = (
        'MATCH (o:Option {uid: $option_uid})'
        '-[p:ParameterTo]'
        '->(d:Disease) '
        'RETURN p.freq as freq, p.impt as impt, p.spec as spec, d.name as disease'
    )
    result = tx.run(query, option_uid=option_uid)
    option_param_list = []
    for record in result:
        option_param_list.append({'freq': record['freq'],
                                  'impt': record['impt'],
                                  'spec': record['spec'],
                                  'disease': record['disease']})
    return option_param_list


def query_detailed_disease_list_by_symptom(symptom):
    """
    根据 symptom 去查找
    基于 symptom -> property -> option -> disease 这条链相关的疾病的属性、选项、参数的详情列表
    """
    with neo_cnx.get_session() as session:
        detailed_disease_list = []
        property_list = session.read_transaction(_query_property_by_symptom, symptom)
        for property_item in property_list:
            option_list = session.read_transaction(_query_option_by_property, property_item['uid'])
            for option_item in option_list:
                option_param_list = session.read_transaction(_query_option_param, option_item['uid'])
                for option_param_item in option_param_list:
                    disease = option_param_item['disease']
                    freq = int(option_param_item['freq'])
                    impt = int(option_param_item['impt'])
                    spec = int(option_param_item['spec'])

                    detailed_disease_list.append({'disease': disease,
                                                  'symptom': symptom,
                                                  'property': property_item['name'],
                                                  'option': option_item['name'],
                                                  'freq': freq,
                                                  'impt': impt,
                                                  'spec': spec,
                                                  'is_inquired': False})

        return detailed_disease_list


def _query_symptom_by_disease(tx, disease_name):
    query = (
        'MATCH (d:Disease {name: $disease_name})'
        '-[:HasSymptom]'
        '->(s:Symptom) '
        'RETURN s.name as name, s.uid as uid'
    )
    result = tx.run(query, disease_name=disease_name)
    symptom_list = []
    for record in result:
        symptom_list.append({'name': record['name'], 'uid': record['uid']})
    return symptom_list


def _query_option_param_to_disease(tx, option_uid, disease_name):
    query = (
        'MATCH (o:Option {uid: $option_uid})'
        '-[p:ParameterTo]'
        '->(d:Disease {name: $disease_name}) '
        'RETURN p.freq as freq, p.impt as impt, p.spec as spec'
    )
    result = tx.run(query, option_uid=option_uid, disease_name=disease_name)
    option_param_list = []
    for record in result:
        option_param_list.append({'freq': record['freq'],
                                  'impt': record['impt'],
                                  'spec': record['spec']})
    return option_param_list


def query_detailed_disease_list_by_disease(disease):
    with neo_cnx.get_session() as session:
        detailed_disease_list = []
        symptom_list = session.read_transaction(_query_symptom_by_disease, disease)
        for symptom_item in symptom_list:
            symptom = symptom_item['name']
            property_list = session.read_transaction(_query_property_by_symptom, symptom)
            for property_item in property_list:
                option_list = session.read_transaction(_query_option_by_property, property_item['uid'])
                for option_item in option_list:
                    option_param_list = session.read_transaction(_query_option_param_to_disease,
                                                                 option_item['uid'], disease)
                    for option_param_item in option_param_list:
                        freq = int(option_param_item['freq'])
                        impt = int(option_param_item['impt'])
                        spec = int(option_param_item['spec'])

                        detailed_disease_list.append({'disease': disease,
                                                      'symptom': symptom,
                                                      'property': property_item['name'],
                                                      'option': option_item['name'],
                                                      'freq': freq,
                                                      'impt': impt,
                                                      'spec': spec,
                                                      'is_inquired': False})

        return detailed_disease_list


if __name__ == '__main__':
    detailed_disease_list = query_detailed_disease_list_by_symptom('咯血')
    print(detailed_disease_list)

    detailed_disease_list = query_detailed_disease_list_by_disease('支气管扩张')
    print(detailed_disease_list)

    neo_cnx.close()
